<?php

namespace Celebrio\Util;

use Nette\Object;
use GettextExtractor\Translator;
use Nette\Environment;

/**
 * @author pavel
 */
class Localization extends Object {

    private static $full_lang;
    private static $lang;

    /**
     * @var \Nette\ITranslator
     */
    private static $translator;

    public static function getLang() {
        return self::$lang;
    }

    public static function getFullLang() {
        return self::$full_lang;
    }

    public static function getTranslator() {
        return self::$translator;
    }

    public static function supportedLanguages() {
        return array(
            "en_US" => "English",
            "cs_CZ" => "Čeština",
            "sk_SK" => "Slovenčina"
        );
    }

    /**
     * Sets up the language according to the preferences of the user which is logged in
     *
     * @static
     * @param null|string $lang short lang (cs, en, ...)
     * @return array
     */
    public static function setup($lang = null) {
        $user = Environment::getUser();

        if ($lang === null) {
            if ($user->isLoggedIn() && array_key_exists("language", $user->getIdentity()->data)) {
                self::$full_lang = $user->getIdentity()->data["language"];
                $lang = substr(self::$full_lang, 0, 2);
            } else {
                $lang = self::preferredLanguage();
            }
        }

        Environment::setVariable('lang', $lang);
        if(self::$full_lang === null) self::$full_lang = Translator::$unix_locales[$lang];

        self::$lang = $lang;
        self::$translator = new Translator(Environment::getVariable('lang'), realpath(APP_DIR . '/locale'));

        return array("lang" => $lang, "full_lang" => self::$full_lang, "translator" => self::$translator);
    }

    /**
     * Sets the translator to the given template so that it's possible to use
     * {_"this syntax"} to get the translated string. Actually it's translate()
     * method.
     *
     * @static
     * @param \Nette\Templates\ITemplate $template
     * @param null $lang
     * @return \Nette\Templates\ITemplate
     */
    public static function setTranslator(\Nette\Templates\ITemplate $template) {
        // set language and gettext
        $template->full_lang = self::$full_lang;
        $template->lang = self::$lang;
        $template->setTranslator(self::$translator);
        return $template;
    }

    /**
     * find preferred language
     *
     * @static
     * @return string short lang, for example "en" or "cs"
     */
    private static function preferredLanguage() {

        if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && ($list = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']))) {
            if (preg_match_all('/([a-z]{1,8}(?:-[a-z]{1,8})?)(?:;q=([0-9.]+))?/', $list, $list)) {
                $langlist = array_combine($list[1], $list[2]);
                foreach ($langlist as $n => $v) {
                    $langlist[$n] = +$v ? +$v : 1;
                }
                arsort($langlist);

                while ($lang = key($langlist)) {
                    if (array_key_exists($lang, Translator::$supported_languages)) {
                        return $lang;
                    }
                    next($langlist);
                }
            }
        }
        return "en"; // u.s. english is default
    }

}